package com.sky.controller;

import com.itheima.aliyun.AliyunOSSUtils;
import com.sky.result.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/admin/common")
public class UploadController {
    @Autowired
    private AliyunOSSUtils aliOssUtil;

    @PostMapping("upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws Exception {
        //拿到文件名
        String originalFilename = file.getOriginalFilename();
        // 拿到上传的文件的后缀
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 拿到上传的文件的字节数组
        byte[] content = file.getBytes();
        // 调用util上传到aliyun oss
        String url = aliOssUtil.upload(content, extName);
        return Result.success(url);
    }

}
